<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j" %>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>user login</title>
</head>


<link rel="stylesheet" href="../css/css1.css" type="text/css" />
<link rel="stylesheet" href="../js/extjs/resources/css/ext-all.css" type="text/css" />
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/validate.js"></script>
<script type="text/javascript" src="../js/extjs/ext-all-debug.js"></script>
<script type="text/javascript" src="../js/extjs/ext-lang-zh_CN.js"></script>
	<script>
	<!--
		//
		window.onload = function(){
			//如果当前session中有user对象,则清除(相当于注销)
			clearSession();
			//页面加载时,调更新所有admin生日的方法
			//updateAdminClick();
			//验证码
			changeCode();
		}
		
		function updateAdminClick() {
	    	document.getElementById("loginForm:updateAdmin").click();
		}
		
		//如果当前session中.session.getA("currentLoginInfo")!=null,则清除当前session中的user
		function clearSession() {
			document.getElementById('loginForm:clearSessionBtn').click();
		}
		
		//验证码 
		var i = 1;
		function changeCode(){
			var path = document.getElementById("loginForm:path").value;
			//alert(path);
			i++;
			document.getElementById("code").src = path + "checkcode?"+i;
		}

		//richcalendar控制时间js
		function preventAheadTime(day) {
			var curDt = new Date();
			if ((curDt.getTime() - day.date.getTime()) < 0) {
				return false;
			} else {
				return true;
			}
		}
		function loginCheck() {
			var loginFlag = document.getElementById("loginForm:loginFlag").value;		//1:login suceed 0:fail
			//alert("loginFlag = " + loginFlag);
			if(loginFlag == 1) {//normal user login succeed
				alert('user login succeed');
				//forward to "pressure-list.jsp" to managed his PRESSRES
				document.getElementById("loginForm:loginBtnUser").click();
			} else if(loginFlag == 2) {	
				alert("admin login succeed");
				//forward to "user-list.jsp" to managed USERS
				document.getElementById("loginForm:loginBtnAdmin").click();
				return;
			} else {
				alert("sorry!login failed!plz check input content!");
			}
			
		}
	-->
	</script>

<body>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<f:view>
<a4j:form id="loginForm">

			<p align = "center">
				user - login
			</p>
	<table id="loginTable" align="center">
		<tr>
			<td class="TxtRight">
				<h:outputLabel>email</h:outputLabel>
			</td>
			<td class="TxtLeft">
				<h:inputText id="name" value="#{UserManagedBean.user.email }" />
			</td>
		</tr>

		<tr>
			<td class="TxtRight">
				<h:outputLabel>password</h:outputLabel>
			</td>
			<td class="TxtLeft">
				<h:inputSecret id="password" value="#{UserManagedBean.user.password }" />
			</td>
		</tr>
		
		<tr>
			<td class="TxtRight">
				<h:outputLabel>birthdate</h:outputLabel>
			</td>
			<td class="TxtLeft">
				<rich:calendar id="birthdate" 
					value="#{UserManagedBean.user.birthdate }"
					isDayEnabled="preventAheadTime"
					datePattern="yyyy-MM-dd" popup="true" inputSize="23"
					inputClass="inputText" enableManualInput="false" 
				/>
			</td>
		</tr>
		<tr>
			<td valign=middle class=tablebody1>checkCode</td>
			<td valign=middle class=tablebody1><input type='text' name="code" size="8" />&nbsp;&nbsp;&nbsp;
				<a href="javascript:changeCode()"><img src="<%=path %>/checkcode" id="code" />change it</a>
			</td>
		</tr>
		<tr>
			<td>
				<a4j:commandButton id="loginBtn" value="login" styleClass="btn"
					action="#{UserManagedBean.loginCheck}"  
					reRender="loginForm:loginFlag"
					oncomplete="loginCheck();"
					onmouseover="mOverBtn(this,'Find')"
					onmouseout="mOutBtn(this,'Find')"/>
				<!-- if adminAccount logined , forward to "user-list" -->
				<a4j:commandButton id="loginBtnAdmin" style="display:none" value="loginBtnAdmin"
					action="#{UserManagedBean.forUserList }"/>
				<!-- if userAccount logined , forward to "pressure-list" -->
				<a4j:commandButton id="loginBtnUser" style="display:none"
					action="#{BloodPressureManagedBean.forBloodPressureList }"/>				
			</td>
			<td>
				<a4j:commandLink id="goRegister" value="register now"
								action="#{UserManagedBean.forUserInsert }">
				</a4j:commandLink>			
			</td>
		</tr>		
	</table>	
			<h:inputHidden id="path" value="/HealthManager1.0/" />
			<h:inputHidden id="loginFlag" value="#{UserManagedBean.loginFlag }"/>
			<h:commandButton id="updateAdmin" action="#{UserManagedBean.updateAdmin }" style="display:none"/>
			<a4j:commandButton id="clearSessionBtn" action="#{UserManagedBean.clearSession }" style="display:none"/>
</a4j:form>
</f:view>
</body>
<script type="text/javascript">
</script>
</html>